#pragma once
#include <vector>
#include "io_thread.h"
#include <atomic>
#include <memory>

struct ThreadLoad {
    size_t conn_num;
    size_t index;
    bool operator>(const ThreadLoad &other) const {
        return conn_num > other.conn_num; // 最小堆
    }
};

class EventLoop{
public:
    EventLoop(int thread_num);
    ~EventLoop();

    void NotifyNewCons(std::vector<int> &conns);
    void StopIOThread();
    
    // 更新某个线程的连接数
    void update_thread_load(size_t index, size_t conn_num);
    

private:
    //查找最空闲的线程
    std::shared_ptr<IOThread> get_idle_thread(size_t &out_index);

    std::vector<std::shared_ptr<IOThread>> _work_threads;
    std::priority_queue<ThreadLoad, std::vector<ThreadLoad>, std::greater<>> _min_heap;
    std::mutex _heap_mtx;
    std::atomic<size_t> _next_idx;
    int _thread_num;
};
